package cn.com.cloudpioneer.evaluate.service;

import cn.com.cloudpioneer.evaluate.mapper.ItemMapper;
import cn.com.cloudpioneer.evaluate.mapper.TargetMapper;
import cn.com.cloudpioneer.evaluate.mapper.TaskMapper;
import cn.com.cloudpioneer.evaluate.model.ItemModel;
import cn.com.cloudpioneer.evaluate.model.ResourceModel;
import cn.com.cloudpioneer.evaluate.model.TargetModel;
import cn.com.cloudpioneer.evaluate.model.TaskModel;
import cn.com.cloudpioneer.evaluate.utils.CustomException;
import cn.com.cloudpioneer.evaluate.utils.Generate;
import cn.com.cloudpioneer.evaluate.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

/**
 * @auther maruikai
 * @create 2023/9/20-11:30
 */
@Service
public class TaskService {

    @Resource
    private TaskMapper taskMapper;
    @Resource
    private ItemMapper itemMapper;
    @Resource
    private TargetMapper targetMapper;

    @Autowired
    private TargetService targetService;
    @Autowired
    private ResourceService resourceService;
    @Autowired
    private FileService fileService;

    @Transactional(rollbackFor = Exception.class)
    public Object setTask(TaskModel taskModel) throws CustomException {
        int flag;
        if (StringUtil.isEmpty(taskModel.getTaskId())) {
            taskModel.setTaskId(Generate.generateId());
            if (StringUtil.isEmpty(taskModel.getParentId())) {
                taskModel.setParentId(null);
            }
            flag = taskMapper.addTask(taskModel);
        } else {
            flag = taskMapper.update(taskModel);
        }
        if (flag == 0) {
            throw new CustomException(5001, "操作失败");
        }
        return flag;
    }

    public Object delTask(String taskId) throws CustomException {
        TaskModel byId = taskMapper.findById(taskId);
        if (byId == null) {
            throw new CustomException(5005, "无法找到此任务!");
        }
        return taskMapper.delTask(true, byId.getTaskId());
    }

    public List<TaskModel> getTaskList(String itemId) throws CustomException {
        ItemModel itemModel = Optional.of(itemId).map(itemMapper::findById).orElseThrow(CustomException::new);
        List<TaskModel> parentByItemIn = taskMapper.findParentByItemId(itemModel.getItemId());
        for (TaskModel taskModel : parentByItemIn) {
            List<TaskModel> byParentIt = taskMapper.findByParentIt(taskModel.getTaskId());
            if (byParentIt.size() > 0) {
                for (TaskModel model : byParentIt) {
                    List<TargetModel> byTaskId = targetMapper.findByTaskId(model.getTaskId());
                    int court = 0;
                    for (TargetModel targetModel : byTaskId) {
                        if (!StringUtil.isEmpty(targetModel.getResult())
                                && !StringUtil.isEmpty(targetModel.getRemark())
                                && !StringUtil.isEmpty(targetModel.getScore())) {
                            court++;
                        }
                    }
                    if (court != 0) {
                        double v = formatNum(court, byTaskId.size());
                        model.setSchedule(v);
                    }
                    model.setTargetList(byTaskId);
                }
                taskModel.setChildTask(byParentIt);
            }
        }
        return parentByItemIn;
    }

    public Object getTaskChileList(String taskId) {
        List<TaskModel> byParentIt = taskMapper.findByParentIt(taskId);
        for (TaskModel taskModel : byParentIt) {
            double schedule = this.getSchedule(taskModel.getTaskId());
            taskModel.setSchedule(schedule);
        }
        return byParentIt;
    }

    public Object getTargetList(String taskId) {
        return targetService.getTargetList(taskId);
    }

    public double getSchedule(String taskId) {
        List<TargetModel> byTaskId = targetMapper.findByTaskId(taskId);
        int court = 0;
        for (TargetModel targetModel : byTaskId) {
            if (!StringUtil.isEmpty(targetModel.getResult())
                    && !StringUtil.isEmpty(targetModel.getRemark())
                    && !StringUtil.isEmpty(targetModel.getScore())) {
                court++;
            }
        }
        if (court != 0) {
            return formatNum(court, byTaskId.size());
        }
        return 0;
    }

    public Object getDocumentList(String taskId) throws CustomException {

        TaskModel byId = taskMapper.findById(taskId);
        if (byId == null) {
            throw new CustomException(5005, "无法找到此任务!");
        }
        List<ResourceModel> resList = resourceService.findByObjId(taskId);
        for (ResourceModel resourceModel : resList) {
            if (!StringUtil.isEmpty(resourceModel.getFilePath())) {
                String viewUrl = fileService.formatViewUrl(resourceModel.getFilePath());
                resourceModel.setPreviewUrl(viewUrl);
            }
        }
        return resList;
    }

    public double formatNum(int a, int b) {
        return (new BigDecimal((float) a / b).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue()) * 100;
    }

}
